package g2.routemaster.gui.actions;

import g2.routemaster.gui.BoardSingleton;
import g2.routemaster.gui.GManager;
import g2.routemaster.model.Board;
import g2.routemaster.model.GameManager;
import g2.routemaster.model.Player;

import java.util.ArrayList;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.window.ApplicationWindow;

public class RestartGameAction extends Action {
   private ApplicationWindow window;
   public RestartGameAction(ApplicationWindow window) {
      setText("Restart Game");
      setToolTipText("Restart Game");
      this.window = window;
   }
   
   public void run() {
      GameManager gm = GManager.instance().getGameManager();
      ArrayList<Player> players = new ArrayList<Player>();
      Player firstPlayer = gm.getFirstPlayer();
      Player player = firstPlayer;
      while(player != null){
         players.add(player);
         if(gm.getNextPlayer(player).equals(firstPlayer)){
            break;
         }
         player = gm.getNextPlayer(player);
      }
      gm.setGameStarted(false);
      gm.setTurnManager(null);
      gm.setup();
      for(Player newPlayer : players){
         newPlayer.setSeatPlace(null);
         gm.insertPlayer(newPlayer.getName(), newPlayer.getBirth(), newPlayer.getColor().getName());
      }
      
      gm.startGame();
      
      BoardSingleton.instance().updateAll();
   }
}
